/**

This file is part of MaCI/GIMnet.

MaCI/GIMnet is free software: you can redistribute it and/or modify it 
under the terms of the GNU Lesser General Public License as published 
by the Free Software Foundation, either version 3 of the License, or 
(at your option) any later version.

MaCI/GIMnet is distributed in the hope that it will be useful, but WITHOUT 
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public 
License for more details.

You should have received a copy of the GNU Lesser General Public 
License along with GIMnet. (See COPYING.LESSER) If not, see 
<http://www.gnu.org/licenses/>.

**/
// -*- coding:iso-8859-15 mode: c++ -*-
/** 
 * \file
 * \brief MaCI_IMU Player for logged files.
 * \author Matthieu Myrsky <matthieu.myrsky@tkk.fi>
 *
 * $Id: IMUFilePlayer.cpp,v 1.1 2009-06-29 13:01:54 morsko Exp $
 *
 */

#include "IMUFilePlayer.hpp"

IMUFilePlayer::IMUFilePlayer( MaCI::IMU::CIMUServer *aIMUServer,
                              std::string aFileName)
  :PlayerModule(),
   iIMUServer(aIMUServer),
   iData(),
   iTimes(),
   iBegin(0,0),
   iEnd(0,0),
   iFile(NULL),
   iName("")
{
 
  
  dPrint(3,"Opening file '%s'",aFileName.c_str());
  iFile= fopen(aFileName.c_str(),"rt");
  char c[200];
  char interfaceName[64];
  //Read the fourteen first lines..and get the interfacename...
  fgets (c, 200,iFile );

  fscanf(iFile,"%s %s %s %s",c,c,c,interfaceName);
  for(int i = 0; i< 12; i++){
    fgets (c, 200,iFile );
  }


  iName = "MaCI_IMU."+std::string(interfaceName);

  ReadData();
  MaCI::IMU::TEventTypes types;
  types = 1<<MaCI::IMU::KTypeAcceleration3d | 
    1<<MaCI::IMU::KTypeAngle3d |
    1<<MaCI::IMU::KTypeAngularVelocity3d |
    1<<MaCI::IMU::KTypeQuaternion | 
    1<<MaCI::IMU::KTypeOrientationMatrix;

  iIMUServer->SetInterfaceInstanceName(std::string(interfaceName));
  iIMUServer->SetEventTypes(types);
  iIMUServer->Open();
}
//*****************************************************************************

IMUFilePlayer::~IMUFilePlayer()
{
  fclose(iFile);
  delete(iIMUServer);
}
//*****************************************************************************

bool IMUFilePlayer::ReadData()
{
  using namespace MaCI::IMU;
  MaCI::Common::TTimestamp timestamp;
  MaCI::Common::TTimestamp lastTimestamp;
  int number = 1;
  unsigned int command;
  bool first = true;
  MaCI::IMU::CIMUData *imudata = NULL;
  while(number != EOF){
    lastTimestamp = timestamp;
    number = fscanf(iFile,"%u %u %u",
                    &command,
                    &(timestamp.timestamp_s),
                    &(timestamp.timestamp_us));
   
    if(first && number == 3){
      
      iBegin = timestamp.GetGimTime();
      first = false;
    }


    if(lastTimestamp.timestamp_s != timestamp.timestamp_s ||
       lastTimestamp.timestamp_us != timestamp.timestamp_us){
      if(imudata != NULL){
        iTimes.push_back(lastTimestamp.GetGimTime());
        iData[lastTimestamp.GetGimTime()] = imudata;
      }

      imudata = new CIMUData();
      imudata->CreateInternalBinBag();
      imudata->SetCommand(TCommand(KCommandEvent));
      imudata->SetTimestamp(timestamp);    
    }
    //    dPrint(1,"timestamp %s, number %d",timestamp.GetGimTime().toString().c_str(),number);




    if(number >0){

      switch(command){
      case 0:{
        float accX,accY,accZ;
        number = fscanf(iFile,"%f %f %f",
                        &accX,
                        &accY,
                        &accZ);
        imudata->SetAcceleration3d(TAcceleration3d(accX,
                                                   accY,
                                                   accZ));
        break;
      }
      case 1:{
        float rotX,rotY,rotZ;
        number = fscanf(iFile,"%f %f %f",
                        &rotX,
                        &rotY,
                        &rotZ);
        imudata->SetAngularVelocity3d(TAngularVelocity3d(rotX,
                                                         rotY,
                                                         rotZ));
        
        break;
      }
      case 2:{
        float roll,pitch,yaw;
        number = fscanf(iFile,"%f %f %f",
                        &roll,
                        &pitch,
                        &yaw);
        imudata->SetAngle3d(TAngle3d(roll,pitch,yaw));
        break;
      }
      case 3:{
        float x,y,z,scalar;
        number = fscanf(iFile,"%f %f %f %f",
                        &scalar,
                        &x,
                        &y,
                        &z);

        imudata->SetQuaternion(TQuaternion(scalar,
                                           x,
                                           y,
                                           z));
        break;
      }
      case 4:{
        float e11,e12,e13,e21,e22,e23,e31,e32,e33;
        number = fscanf(iFile,"%f %f %f %f %f %f %f %f %f",
                        &e11,&e12,&e13,
                        &e21,&e22,&e23,
                        &e31,&e32,&e33);
        imudata->SetOrientationMatrix(TOrientationMatrix(e11,e12,e13,
                                                         e21,e22,e23,
                                                         e31,e32,e33));
        break;
      }
        
        
      }

     
     
    
    }
    

  }
  if(imudata != NULL){
    iTimes.push_back(timestamp.GetGimTime());
    iData[lastTimestamp.GetGimTime()] = imudata;
  }
  if(!first){
    iEnd = timestamp.GetGimTime();
  }

  return true;

}
//*****************************************************************************

bool IMUFilePlayer::OnTime(gim::time aTime)
{
  std::map<gim::time,MaCI::IMU::CIMUData *>::iterator iter
    =iData.find(aTime);



  if(iter != iData.end()){
    iIMUServer->SendEvent(*(iter->second));
    return true;
  }else{

    return false;
  }

}
//*****************************************************************************

bool IMUFilePlayer::GetTimes(std::list<gim::time> *aTimes)
{

  aTimes->assign(iTimes.begin(),iTimes.end());
  return true;
}
//*****************************************************************************

std::string IMUFilePlayer::GetName(void)
{
  return iName;

}
//*****************************************************************************

void IMUFilePlayer::GetTimeLimits(gim::time &aBegin,gim::time &aEnd)
{
  aBegin = iBegin;
  aEnd = iEnd;
  dPrint(1,"b %s, e %s", aBegin.toString().c_str(), aEnd.toString().c_str());
}
//*****************************************************************************




